{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# init func\n",
    "\n",
    "import RPi.GPIO as GPIO\n",
    "import time\n",
    "\n",
    "def init(ch_list):\n",
    "    GPIO.setmode(GPIO.BCM)\n",
    "    GPIO.setup(ch_list, GPIO.OUT)\n",
    "    GPIO.output(ch_list, GPIO.LOW)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# two modes func: check or scan\n",
    "def pulse(ch, interv_nsec):\n",
    "    GPIO.output(ch, GPIO.HIGH)\n",
    "    time.sleep(interv_nsec)\n",
    "    GPIO.output(ch, GPIO.LOW)\n",
    "\n",
    "def check(ch_list, interv_nsec):\n",
    "    while(1):\n",
    "        idx = int(input('pulse at channel: '))\n",
    "        if idx < 0 or idx > len(ch_list)-1:\n",
    "            break\n",
    "        else:\n",
    "            pulse(ch_list[idx], interv_nsec)\n",
    "\n",
    "def scan(ch_list, interv_nsec):\n",
    "    for ch in ch_list:\n",
    "        pulse(ch, interv_nsec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "choose mode (check nsec or scan nsec or exit)check 2\n",
      "pulse at channel: 4\n",
      "pulse at channel: -1\n",
      "choose mode (check nsec or scan nsec or exit)scan\n",
      "choose mode (check nsec or scan nsec or exit)scan 0.2\n",
      "choose mode (check nsec or scan nsec or exit)check 0.5\n",
      "pulse at channel: 0\n",
      "pulse at channel: 1\n",
      "pulse at channel: 2\n",
      "pulse at channel: 3\n",
      "pulse at channel: 4\n",
      "pulse at channel: 6\n",
      "pulse at channel: 7\n",
      "pulse at channel: 8\n",
      "pulse at channel: 9\n",
      "pulse at channel: 10\n",
      "pulse at channel: 11\n",
      "pulse at channel: 12\n",
      "pulse at channel: 13\n",
      "pulse at channel: -1\n",
      "choose mode (check nsec or scan nsec or exit)exit\n"
     ]
    }
   ],
   "source": [
    "# choice loop\n",
    "def choose_loop(ch_list):\n",
    "    interv_nsec = 1\n",
    "    while(1):\n",
    "        cmd = input('choose mode (check nsec or scan nsec or exit)')\n",
    "        cmd_details = cmd.split(' ')\n",
    "        if cmd_details[0] == 'check':\n",
    "            if len(cmd_details) < 2:\n",
    "                check(ch_list, interv_nsec)\n",
    "            else:\n",
    "                check(ch_list, float(cmd_details[1]))\n",
    "        elif cmd_details[0] == 'scan':\n",
    "            if len(cmd_details) < 2:\n",
    "                scan(ch_list, interv_nsec)\n",
    "            else:\n",
    "                scan(ch_list, float(cmd_details[1]))\n",
    "        elif cmd_details[0] == 'exit':\n",
    "            break\n",
    "        else:\n",
    "            print('cmd not found.')\n",
    "\n",
    "if __name__=='__main__':\n",
    "    ch_list = list(range(2, 18))\n",
    "    init(ch_list)\n",
    "    choose_loop(ch_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
